import request from '@/utils/request'

// 用户登录
export function login(data: { username: string; password: string }) {
  return request({
    url: '/user/login',
    method: 'post',
    data
  })
}

// 用户注册
export function register(data: any) {
  return request({
    url: '/user/register',
    method: 'post',
    data
  })
}

// 获取用户信息
export function getUserInfo() {
  return request({
    url: '/user/info',
    method: 'get'
  })
}

// 获取商品列表
export function getProducts(params: any) {
  return request({
    url: '/products',
    method: 'get',
    params
  })
}

// 获取商品详情
export function getProductDetail(id: number) {
  return request({
    url: `/products/${id}`,
    method: 'get'
  })
}

// 获取进行中的促销活动
export function getActivePromotions() {
  return request({
    url: '/promotions/active',
    method: 'get'
  })
}

// 获取商品的促销信息
export function getProductPromotion(productId: number) {
  return request({
    url: `/promotions/product/${productId}`,
    method: 'get'
  })
}

// 获取订单列表
export function getOrders(params?: any) {
  return request({
    url: '/user/orders',
    method: 'get',
    params
  })
}

// 获取订单详情
export function getOrderDetail(orderNo: string) {
  return request({
    url: `/user/orders/${orderNo}`,
    method: 'get'
  })
}

// 取消订单
export function cancelOrder(orderNo: string) {
  return request({
    url: `/user/orders/${orderNo}/cancel`,
    method: 'post'
  })
}

// 支付订单
export function payOrder(orderNo: string) {
  return request({
    url: `/user/balance/pay/${orderNo}`,
    method: 'post'
  })
}

// 获取地址列表
export function getAddresses() {
  return request({
    url: '/user/addresses',
    method: 'get'
  })
}

// 添加地址
export function addAddress(data: any) {
  return request({
    url: '/user/addresses',
    method: 'post',
    data
  })
}

// 更新地址
export function updateAddress(id: number, data: any) {
  return request({
    url: `/user/addresses/${id}`,
    method: 'put',
    data
  })
}

// 删除地址
export function deleteAddress(id: number) {
  return request({
    url: `/user/addresses/${id}`,
    method: 'delete'
  })
}

// 修改密码
export function updatePassword(data: { oldPassword: string; newPassword: string }) {
  return request({
    url: '/user/password',
    method: 'put',
    data
  })
}

// 更新用户信息
export function updateUserInfo(data: any) {
  return request({
    url: '/user/info',
    method: 'put',
    data
  })
}

// 获取用户余额
export function getUserBalance() {
  return request({
    url: '/user/balance',
    method: 'get'
  })
}

// 充值/提现
export function operateBalance(data: any) {
  return request({
    url: '/user/balance/operate',
    method: 'post',
    data
  })
}
